package org.nf.trading.service.app.product.impl;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.dao.FavoriteMapper;
import org.nf.trading.entity.Favorite;
import org.nf.trading.exception.app.product.ProductDataException;
import org.nf.trading.service.app.product.FavoriteService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
@Transactional
public class FavoriteServiceImpl implements FavoriteService {

    private final FavoriteMapper favoriteMapper;

    /**
     * 通过用户id查询全部商品
     * @param uid
     * @return
     */
    @Override
    public List<Favorite> listFavoritesByUid(Integer uid) {
        if (uid == null){
            log.info("用户id为空");
            throw new ProductDataException(500,"用户id为空");
        }
        return favoriteMapper.listFavoritesByUid(uid);
    }

    /**
     *  查询用户收藏商品数量
     * @param uid
     * @return
     */
    @Override
    public Long countByUId(Integer uid) {
        if (uid == null){
            throw new ProductDataException(500,"用户id为空");
        }
        return favoriteMapper.countByUId(uid);
    }

    /**
     * 收藏商品
     * @param record
     * @return
     */
    @Override
    public boolean insert(Favorite record) {
        if (record == null){
            log.error("添加收藏失败，参数为空！");
            throw new ProductDataException(500,"添加收藏失败，参数为空！");
        }
        int insert = favoriteMapper.insert(record);
        Integer favoriteId = record.getFavoriteId();
        log.info("收藏表的id为：{}",favoriteId);
        return insert == 1;
    }


}
